var roleManage = function() {

    var _init = function() {

        var table = $('#table_role');
        var parentId=$('.ids').val();

        var tableUnit=table.dataTable({
            "processing": true,
            // 开启服务器模式
            "serverSide": true,
            "ajax": {
            	url:"/a/biz/detaiLAgents",
            	data:{"parentId":parentId}
            },
            "autoWidth": false,
            "columns": [   {
                "width": "10%",
                "data": "createDate",
                "render": function(data, type, row, meta) {
                    return moment(data).format('YYYY-MM-DD');
                }
            },{
                    "width": "10%",
                    "data": "name"
                },
                {
                    "width": "8%",
                    "data": "levelId",
                    "render": function(data, type, row, meta) {
                    	var val=data;
                    	if(data!=null&&data!=''){
            				$.ajax({
            					url : '/a/biz/alevel/obj',
            					type : 'post',
            					dataType : 'json',
            					contentType:"application/json; charset=utf-8",
            					async: false,
            					data : JSON.stringify({ id: data }),
            				}).done(function(r) {
            					if (r.status === '1') {
            						val=r.data.name;

            					} else if (r.status === '2') {
            					}
            				});
            				return val;
                    	}else
                    		return "";	
                    }
                },
                {
                    "width": "10%",
                    "data": "type"
                },
                {
                    "width": "7%",
                    "data": "contractName"
                },
                {
                    "width": "10%",
                    "data": "phone"
                },
                {
                    "width": "7%",
                    "data": "null",
                        "render": function(data, type, row, meta) {
                        	var val=0;
                				$.ajax({
                					url : '/a/biz/countTenant',
                					type : 'post',
                					dataType : 'json',
                					contentType:"application/json; charset=utf-8",
                					async: false,
                					data : JSON.stringify({ 'agentId': row.id }),
                				}).done(function(r) {
                					if (r.status === '1') {
                						val=r.data;

                					} else if (r.status === '2') {
                					}
                				});
                				return val;
                        }
                },
            
               {
                    "width": "7%",

                    "data": "isActive",
                    "render": function(data, type, row, meta) {
                    	if(data=='0'){
                    		return "停用";	
                    	}else{
                    		return "正常";	
                    	}
                    }
                }, 
             
                {
                    "width": "15%",
                    "data": "null",
                    "render": function(data, type, row, meta) {
                   	   return "<button type=\"button\" class=\"btn  btn_detail_role\">查看详情</button>";
                   }
                }
            ],
            "language": {
                "lengthMenu": "每页显示 _MENU_ 条记录",
                "emptyTable": "暂无数据记录",
                "info": "显示 _START_ 至 _END_ 条数据，共 _TOTAL_ 条记录！",
                "zeroRecords": "没有搜索到匹配记录",
                "infoEmpty": "",
                "infoFiltered": "",
                "processing": "数据加载中,请稍后..."

            },
            "pageLength": 5,
            "order": [
                [0, "desc"]
            ],
            "columnDefs": [{
                "orderable": false,
                "targets": [ 8]
            }],
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>p>",
            "fnInitComplete": function() {},
            "fnDrawCallback": function(){
          	}
        });

 

        
        // 详情
        table.on('click', '.btn_detail_role', function(event) {
            var oData_arr = table.DataTable().rows($(this).parents("tr"))
                .data(); // 操作行对象
            var _id = oData_arr[0].id;
        	window.location.href="/p/biz/agent/detail?id="+ encodeURIComponent(_id);
        });
        
        
        
		var tenant_table = $('#tenant_table');
		tenant_table
				.dataTable({
					"processing" : true,
					// 开启服务器模式
					serverSide : true,
					"ajax" : {
						url : "/a/biz/getTenants",
						data:{"agentId":parentId}
					},
					"autoWidth" : false,
					"columns" : [
							{
								"width" : "10%",
								"data" : "name"
							},
							{
								"width" : "10%",
								"data" : "templateId"
							},
							{
								"width" : "10%",
								"data" : "code"
							},
							{
								"width" : "10%",
								"data" : "phone"
							},
						
							{
								"width" : "10%",
								"data" : "province"
							},
							{
								"width" : "10%",
								"data" : "city"
							},
						
						
							{
								"width" : "10%",
								"data" : "createDate",
								"render" : function(data, type, row, meta) {
									return moment(data).format(
											'YYYY-MM-DD');
								}
							},
							{
								"width" : "10%",
								"data" : "stopUseDate",
								"render" : function(data, type, row, meta) {
									if(data){
									return moment(data).format(
											'YYYY-MM-DD');
									}else{
										return "--";
									}
								}
							},
							{
								"width" : "10%",
								"data" : "nums"
							},
							{
								"width" : "7%",

								"data" : "isActive",
								"render" : function(data, type, row, meta) {
									if (data == '0') {
										return "停用";
									} else if(data == '1') {
										return "正常";
									}else{
										return "已过期";
									}
								}
							},

						],
					"language" : {
						"lengthMenu" : "每页显示 _MENU_ 条记录",
						"emptyTable" : "暂无数据记录",
						"info" : "显示 _START_ 至 _END_ 条数据，共 _TOTAL_ 条记录！",
						"zeroRecords" : "没有搜索到匹配记录",
						"infoEmpty" : "",
						"infoFiltered" : "",
						"processing" : ""
					},
					"destroy" : true,
					"pageLength" : 5,
					"order" : [ [ 6, "desc" ] ],
					"columnDefs" : [
							{
								"orderable" : false,
								"targets" : [ 8 ]
							} ],
					"dom" : "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>"
							+ "<t>" + "<'row'<'col-md-5'i><'col-md-7'>p>",
					"fnInitComplete" : function() {
					}
				});
            
 
    }

    return {
        init: function() {
            _init();
        }

    };

}();

$('#agent_jbzl_eidt').on('click', function(event) {
	 var _id=$('.ids').val();
	window.location.href="/p/biz/agent/edit?id="+ encodeURIComponent(_id);
});


$(function() {
    roleManage.init();

});